package com.wenjie.controller;

import com.wenjie.original.entity.ScanRecord;
import com.wenjie.original.entity.common.vo.RestBaseVo;
import com.wenjie.original.entity.table.ScanRecordTableDef;
import com.wenjie.original.entity.vo.ScanRecord.ScanRecordUpOrAddVo;
import com.wenjie.original.entity.vo.ScanRecord.ScanRecordVo;
import com.wenjie.original.service.IScanRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("扫描数据")
@RestController
@RequestMapping("/api/scanRecord")
public class ScanRecordController {

    @Autowired
    private IScanRecordService scanService;

    @ApiOperation("查询接口")
    @PostMapping("/list")
    public RestBaseVo<ScanRecordVo.ResVo> list(@RequestBody @Validated ScanRecordVo.SelVo selVo) {
        if (selVo.getCreateDateStart() == null && selVo.getCreateDateEnd() == null) {
            return scanService.list(selVo, ScanRecordVo.ResVo.class);
        } else {
            return scanService.list(selVo, ScanRecordTableDef.SCAN_RECORD.CREATE_TIME.between(selVo.getCreateDateStart(), selVo.getCreateDateEnd()), ScanRecordVo.ResVo.class);
        }
    }

    @ApiOperation("查询接口")
    @PostMapping("/getByCodeByBigScan")
    public RestBaseVo<ScanRecordVo.ResVo> getByCodeByBigScan(@RequestBody @Validated ScanRecordVo.getByCodeByBigScanSelVo selVo) {
        return scanService.getByCodeByBigScan(selVo, ScanRecordVo.ResVo.class);
    }


    @ApiOperation("详情接口--显示记录的分层")
    @PostMapping("/detail")
    public List<ScanRecord> detail(@RequestBody Long scanRecordId) {
        return scanService.detail(scanRecordId);
    }

    @ApiOperation("添加小箱扫码记录")
    @PostMapping("/addsmall")
    public String addsmall(@RequestBody @Validated ScanRecordUpOrAddVo.AddSmallVo addVo) {
        Boolean check = scanService.checkAuthority(addVo.getOrderBoxId());
        if (!check) return "restart";

        scanService.saveSmall(addVo);
        return null;
    }


    @ApiOperation("添加大箱扫码记录")
    @PostMapping("/addBig")
    public String addBig(@RequestBody @Validated ScanRecordUpOrAddVo.AddBigVo addVo) {
        Boolean check = scanService.checkAuthority(addVo.getOrderBoxId());
        if (!check) return "restart";

        scanService.saveBig(addVo);
        return null;
    }

    @ApiOperation("删除接口")
    @PostMapping("/del")
    public void del(@RequestBody @Validated Long id) {
        scanService.removeById(id);
    }


}
